#!/bin/sh

#
# make 3rd library distribute package
#

distroot="dist"
srcroot="install"
disttype="$1"

die() {
    echo "$1"
    exit -1
}

safe_ln() {
    if [ ! -e "$1" ]; then
	die "$1 not exist"
    fi

    ln -s $1 $2
}

make_dist() {
    srcdir="$1"
    dstdir="$2"
    distname="$3"

    installdir="$dstdir/third_party_install"
    if [ -d "$installdir" ]; then
	#rm -rf $installdir
	echo ''
    else
	mkdir -p $installdir
    fi

    rsync="rsync -av --delete"
    $rsync "$srcdir/include" $installdir
    $rsync "$srcdir/lib" $installdir
    $rsync "$srcdir/bin" $installdir
    $rsync "$srcdir/python_package" $installdir
    $rsync "$srcdir/share/swig" $installdir/share

    lastdir=$(pwd)
    cd $installdir
    ln -s include third_party
    cd include
    ln -s python2.7 python
    ln -s google/protobuf protobuf
    cd ../lib

    libs="gflags glog gtest protobuf z"
    for libname in $libs
    do
	echo "link <$libname>"
	safe_ln lib${libname}.a libthird_party_${libname}.a
    done

    cd ../../

    echo "make tarball..."
    tar -czf $distname third_party_install

    cd $lastdir
    [ ! -d "publish" ] && mkdir publish
    mv $dstdir/$distname publish
}

echo "build debug package tarball..."
make_dist "$srcroot/dbg" "$distroot/dbg" "3rd_packages_dbg.tgz"

#if [ -z "$disttype" -o "$disttype" = "dbg" ];
#then
#    echo "build debug package tarball..."
#    make_dist "$srcroot/dbg" "$distroot/dbg" "3rd_packages_dbg.tgz"
#fi

#if [ -z "$disttype" -o "$disttype" = "release" ];
#then
#    echo "build release package tarball..."
#    make_dist "$srcroot/release" "$distroot/release" "3rd_packages_opt.tgz"
#fi
